package com.ruoyi.web.controller.system;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.entity.UmsMember;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.web.cloud_service.MemServiceClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.RegisterBody;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.service.SysRegisterService;
import com.ruoyi.system.service.ISysConfigService;

/**
 * 注册验证
 * 
 * @author ruoyi
 */
@RestController
public class SysRegisterController extends BaseController
{
    @Autowired
    private SysRegisterService registerService;
    @Autowired
    private MemServiceClient serviceClient;

    @Autowired
    private ISysConfigService configService;
    @Autowired
    private ISysUserService userService;


    @PostMapping("/register")
    public AjaxResult register(@RequestBody SysUser user)
    {
        if (!userService.checkUserNameUnique(user))
        {
            return error("新增用户'" + user.getUserName() + "'失败，登录账号已存在");
        }
        else if (StringUtils.isNotEmpty(user.getPhonenumber()) && !userService.checkPhoneUnique(user))
        {
            return error("新增用户'" + user.getUserName() + "'失败，手机号码已存在");
        }
        else if (StringUtils.isNotEmpty(user.getEmail()) && !userService.checkEmailUnique(user))
        {
            return error("新增用户'" + user.getUserName() + "'失败，邮箱账号已存在");
        }
        user.setCreateBy("注册用户");
        user.setPassword(SecurityUtils.encryptPassword(user.getPassword()));
        UmsMember umsMember=new UmsMember();
        umsMember.setId(user.getUserId());
        umsMember.setUserName(user.getUserName());
        umsMember.setPASSWORD(user.getPassword());
        umsMember.setEmail(user.getEmail());
        umsMember.setAvatar(user.getAvatar());
        umsMember.setNickname(user.getNickName());
        serviceClient.addMember(umsMember);


        return toAjax(userService.insertUser(user));
    }

}
